package com.example.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.backend.common.exception.BusinessException;
import com.example.backend.entity.OperationLog;
import com.example.backend.mapper.OperationLogMapper;
import com.example.backend.service.OperationLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class OperationLogServiceImpl implements OperationLogService {
    
    private final OperationLogMapper operationLogMapper;
    
    @Async
    @Override
    public void save(OperationLog log) {
        operationLogMapper.insert(log);
    }
    
    @Override
    public IPage<OperationLog> getOperationLogs(String username, String module, String type,
            LocalDateTime startTime, LocalDateTime endTime, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<OperationLog> wrapper = new LambdaQueryWrapper<OperationLog>()
                .like(StringUtils.hasText(username), OperationLog::getUsername, username)
                .like(StringUtils.hasText(module), OperationLog::getModule, module)
                .like(StringUtils.hasText(type), OperationLog::getType, type)
                .ge(startTime != null, OperationLog::getCreateTime, startTime)
                .le(endTime != null, OperationLog::getCreateTime, endTime)
                .orderByDesc(OperationLog::getCreateTime);
        
        return operationLogMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cleanLogs(Integer days) {
        if (days == null || days <= 0) {
            throw new BusinessException("清理天数必须大于0");
        }
        
        LocalDateTime time = LocalDateTime.now().minusDays(days);
        operationLogMapper.delete(new LambdaQueryWrapper<OperationLog>()
                .le(OperationLog::getCreateTime, time));
    }
    
    @Override
    public byte[] exportLogs(String username, String module, String type,
            LocalDateTime startTime, LocalDateTime endTime) {
        List<OperationLog> logs = operationLogMapper.selectList(
                new LambdaQueryWrapper<OperationLog>()
                        .like(StringUtils.hasText(username), OperationLog::getUsername, username)
                        .like(StringUtils.hasText(module), OperationLog::getModule, module)
                        .like(StringUtils.hasText(type), OperationLog::getType, type)
                        .ge(startTime != null, OperationLog::getCreateTime, startTime)
                        .le(endTime != null, OperationLog::getCreateTime, endTime)
                        .orderByDesc(OperationLog::getCreateTime));
        
        // TODO: 实现Excel导出
        return new byte[0];
    }
} 